package com.example.activity;

import java.io.IOException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.DefaultClientConnection;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Entity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.consts.Consts;
import com.example.dianping.R;
import com.example.entity.Category;
import com.example.entity.ResponseObject;
import com.example.utils.MyUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class AllCategoryActivity extends Activity {

	@ViewInject(R.id.all_category_back)
	TextView all_category_back;
	@ViewInject(R.id.all_category_list)
	ListView all_category_list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.all_category_layout);
		ViewUtils.inject(this);
		new CategoryDataTask().execute();
	}

	public class CategoryDataTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected Void doInBackground(Void... params) {
			HttpClient httpClient = new DefaultHttpClient();
			// "/spi/category"注意这个修改
			HttpPost post = new HttpPost(Consts.Category_Data_URI);
			try {
				HttpResponse response = httpClient.execute(post);
				if (response.getStatusLine().getStatusCode() == 200) {
					String jsonString = EntityUtils.toString(response
							.getEntity());
					parseCategoryDataJson(jsonString);
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			AllCategoryAdapter adapter = new AllCategoryAdapter();
			all_category_list.setAdapter(adapter);
		}
	}

	private void parseCategoryDataJson(String jsonString) {
		Gson gson = new Gson();
		ResponseObject<List<Category>> responseObject = gson.fromJson(
				jsonString, new TypeToken<ResponseObject<List<Category>>>() {
				}.getType());
		List<Category> datas = responseObject.getDatas();

		for (Category category : datas) {
			int position = Integer.parseInt(category.getCategoryId());
			MyUtils.allCategoryNumber[position - 1] = category
					.getNumber();
		}
		
//		for (int i = 0; i < MyUtils.allCategoryNumber.length; i++) {
//			System.out.println(MyUtils.allCategoryNumber[i]);
//		}

	}

	public class AllCategoryAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return MyUtils.allCategroy.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = LayoutInflater.from(parent.getContext()).inflate(
						R.layout.all_category_layout_item, null);
				ViewUtils.inject(viewHolder, convertView);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}

			viewHolder.iv_all_category_item
					.setImageResource(MyUtils.allCategroyImages[position]);
			viewHolder.tv_all_category_item_categoryname
					.setText(MyUtils.allCategroy[position]);
			viewHolder.tv_all_category_item_sellnum
					.setText(MyUtils.allCategoryNumber[position] + "");

			return convertView;
		}

		private class ViewHolder {
			@ViewInject(R.id.iv_all_category_item)
			ImageView iv_all_category_item;
			@ViewInject(R.id.tv_all_category_item_categoryname)
			TextView tv_all_category_item_categoryname;
			@ViewInject(R.id.tv_all_category_item_sellnum)
			TextView tv_all_category_item_sellnum;
		}

	}

	@OnClick(R.id.all_category_back)
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.all_category_back:
			finish();
			break;

		default:
			break;
		}
	}
}
